$(function(){
    var api = '';
	var page = 0,page_size = 10,page_count=''; //
	$.ajax({
		type:"get",
		dataType:'json',
		url: api+"/hotelp/api/get-index-data",
		success:function(data){ //加载首页数据   isClick
//			console.log( data.msg.data )
			var status =  data.status,list="";
			if( status == 1 ){
				page_count = data.msg.page_count
				$.each(data.msg.data, function(n,value) {
					value
					if( value.isClick ){ //点过赞	
						list +='<li class="item"><div class="person_img"><a href="http://activity.veryeast.cn/hotelp/index/share?id='+ value.id +'"><img class="per_person pull_left" src="'+ value.work_img +'" alt="变身前"  /><img class="per_person pull_right" src="'+ value.change_img +'" alt="变身后"  /><img class="vs_img" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/VS.png" alt="VS"/></a></div><div class="description"><div class="left"><p class="name">姓名：'+ value.user_name +'</p><p>编 号：'+ value.id +'</p><p><span class="company_name">'+ value.hotel_name +'</span><span class="job">'+ value.post +'</span></p></div><div class="right"><p class="num_zhan"><img class="dianzhan" data-src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan_activity.png" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan.png"/><span>'+ value.clickz_count +'</span></p><p data-id="'+ value.id +'" data-click = "'+ value.isClick +'" class="dianzhan_btn js_dianzhan_btn dianzhan_btn_has">投TA一票</p></div></div></li>'
					}else{  //没有点过赞
						list +='<li class="item"><div class="person_img"><a href="http://activity.veryeast.cn/hotelp/index/share?id='+ value.id +'"><img class="per_person pull_left" src="'+ value.work_img +'" alt="变身前"  /><img class="per_person pull_right" src="'+ value.change_img +'" alt="变身后" /><img class="vs_img" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/VS.png" alt="VS"/></a></div><div class="description"><div class="left"><p class="name">姓名：'+ value.user_name +'</p><p>编 号：'+ value.id +'</p><p><span class="company_name">'+ value.hotel_name +'</span><span class="job">'+ value.post +'</span></p></div><div class="right"><p class="num_zhan"><img class="dianzhan" data-src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan_activity.png" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan.png"/><span>'+ value.clickz_count +'</span></p><p data-id="'+ value.id +'" data-click = "'+ value.isClick +'" class="dianzhan_btn js_dianzhan_btn">投TA一票</p></div></div></li>'
				
					}
				});
				$('.js_person_list').html( list )
			}
		}
	});

	$('.js_person_list').on('click','.js_dianzhan_btn',function(){ //点赞
		var id =  $(this).data('id');
		var isClick = $(this).attr('data-click'); //判断是否可以点赞
		var $Num = $(this).siblings().find('span');
		var avtiveimg = $(this).siblings().find("img").data('src');
		var _that = $(this);
		//console.log( isClick )
		if( isClick == 1 ){
			return
		}else{
			$.ajax({
				type:"get",
				dataType:'json',
				url:"/hotelp/api/click-zan?id="+id,
				success:function(data){
					var status =  data.status
					if( status ==1 ){
						$Num.html( Number( $Num.html() )+1 );
						_that.addClass('dianzhan_btn_has');
						_that.siblings().addClass('num_zhan_has');
						_that.siblings().find('img').attr('src',avtiveimg);
						_that.attr('data-click',1);
						alert('投票成功')
					}else{
						var msg = data.err_msg.create_time[0];
						alert(msg)
					}
				}
			});
		}
	})
	
	
	$('.js_search_btn').click(function(){ //搜索
		var id = $('.js_key_word').val();
		if( id ){
			$.ajax({
				type:"get",
				dataType:'json',
				url:"/hotelp/api/is-user?id="+id,
				success:function(data){
					var status =  data.status
					console.log( data )
					if( status ==1 ){ //跳转到结果页
						window.location.href = 'http://activity.veryeast.cn/hotelp/index/share?id='+id;
					}else{ //搜索的人不存在
						alert('你搜索的用户不存在')
					}
				}
			});
		}
	});
	
	
	$(window).scroll(function() { //上拉加载
		if ($(document).scrollTop() <= 0) {
		    console.log( '顶部' )
		}
		if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
			 console.log( '地步' )
			page++;//当前需要请求的页数
		    var totalPage = Math.ceil(page_count/10)-1//总的页数;
		    if( page <= totalPage  ){
		    	get_next( page,10)
		    }
		}
	});
	
	
	function get_next(page,page_size){
		$.ajax({
			type:"get",
			dataType:'json',
			url: api+"/hotelp/api/get-index-data?page="+page+"&page_size="+page_size,
			success:function(data){ //加载首页数据
				var status =  data.status,list="";
				if( status == 1 ){
					$.each(data.msg.data, function(n,value) {
						//console.log( value )
						if( value.isClick ){ //点过赞
						list +='<li class="item"><div class="person_img"><a href="http://activity.veryeast.cn/hotelp/index/share?id='+ value.id +'"><img class="per_person pull_left" src="'+ value.work_img +'" alt="变身前" /><img class="per_person pull_right" src="'+ value.change_img +'" alt="变身后" /><img class="vs_img" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/VS.png" alt="VS"/></a></div><div class="description"><div class="left"><p class="name">姓名：'+ value.user_name +'</p><p>编 号：'+ value.id +'</p><p><span class="company_name">'+ value.hotel_name +'</span><span class="job">'+ value.post +'</span></p></div><div class="right"><p class="num_zhan"><img class="dianzhan" data-src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan_activity.png" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan.png"/><span>'+ value.clickz_count +'</span></p><p data-id="'+ value.id +'" data-click = "'+ value.isClick +'" class="dianzhan_btn js_dianzhan_btn dianzhan_btn_has">投TA一票</p></div></div></li>'
					}else{  //没有点过赞
						list +='<li class="item"><div class="person_img"><a href="http://activity.veryeast.cn/hotelp/index/share?id='+ value.id +'"><img class="per_person pull_left" src="'+ value.work_img +'" alt="变身前" /><img class="per_person pull_right" src="'+ value.change_img +'" alt="变身后" /><img class="vs_img" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/VS.png" alt="VS"/></a></div><div class="description"><div class="left"><p class="name">姓名：'+ value.user_name +'</p><p>编 号：'+ value.id +'</p><p><span class="company_name">'+ value.hotel_name +'</span><span class="job">'+ value.post +'</span></p></div><div class="right"><p class="num_zhan"><img class="dianzhan" data-src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan_activity.png" src="http://f3.v.veimg.cn/ve/activity/hotelp/img/dianzhan.png"/><span>'+ value.clickz_count +'</span></p><p data-id="'+ value.id +'" data-click = "'+ value.isClick +'" class="dianzhan_btn js_dianzhan_btn">投TA一票</p></div></div></li>'
				
					}
					});
					$('.js_person_list').append( list )
				}
			}
		});
	}

//onload="this.width< this.height ? $(this).addClass("rotate-90") : null"
})
